export function formatDate(date, format) {
    if (arguments.length === 1) {
        format = date;
        date = new Date();
    }
    let map = {
        y: date.getFullYear(),
        M: (date.getMonth() + 1),
        d: date.getDate(),
        h: date.getHours(),
        m: date.getMinutes(),
        s: date.getSeconds()
    };
    return Reflect.ownKeys(map).reduce((a, b) => {
        return a.replace(new RegExp(`[${b}]+`, "g"), (str) => {
            let value = map[b] + "";
            if (str.length !== value.length) {
                return str.length > value.length ? (Array.apply(null, Array(str.length - value.length)).map(() => "0").join("") + value) : value.substring(str.length);
            } else {
                return value;
            }
        });
    }, format);
}

export function isNumeric(obj) {
    return !isNaN(parseFloat(obj)) && isFinite(obj);
}

export function isEmptyObject(obj) {
    return Reflect.ownKeys(obj).length === 0;
}

export function getFileSize(size, radmon = 2) {
    let v = 0, unit = "BYTE", byteSize = size;
    radmon = radmon || 0;
    if (byteSize >= 1073741824) {
        v = (byteSize / 1073741824).toFixed(radmon);
        unit = "GB";
    } else if (byteSize >= 1048576) {
        v = (byteSize / 1048576).toFixed(radmon);
        unit = "MB";
    } else if (byteSize >= 1024) {
        v = (byteSize / 1024).toFixed(radmon);
        unit = "KB";
    } else {
        v = byteSize;
        unit = "B";
    }
    return `${v} ${unit}`;
}

export function guid() {
    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
        let r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
        return v.toString(16);
    });
}